package io.qar.mq.controller;

import io.qar.mq.dao.OrdersDao;
import io.qar.mq.entity.OrdersEntity;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: qar-cloud
 * @description:
 * @author: yyyyjinying
 * @create: 2023-10-18 14:44
 **/
@RestController
@RequestMapping("rocketmq")
public class RocketMQTransationController {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private OrdersDao ordersDao;

    @GetMapping("auto")
    public Integer zzz(){
        OrdersEntity ordersEntity = new OrdersEntity();
        ordersEntity.setGoods_name("abc");
        ordersEntity.setTotal(123);
        ordersEntity.setUser_id(1);
        int insert = ordersDao.insert(ordersEntity);
        return insert;
    }

    /**
     * 发送事务消息
     */
    @RequestMapping("/sendTransationMessage")
    public void sendTransationMessage(){
        OrdersEntity ordersEntity = new OrdersEntity();
        ordersEntity.setGoods_name("abc");
        ordersEntity.setTotal(123);
        ordersEntity.setUser_id(1);
        TransactionSendResult result = rocketMQTemplate.sendMessageInTransaction("test-topic-transation", MessageBuilder.withPayload(ordersEntity).build(),null);
        //发送状态
        String sendStatus = result.getSendStatus().name();
        //本地事务执行状态
        String localState = result.getLocalTransactionState().name();
        System.out.println("发送状态:"+sendStatus+";本地事务执行状态"+localState);

    }
}
